package com.shiro_demo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author liang
 * 2021/11/24
 */
@RestController
@Controller
public class MyController {

    @PostMapping("/login")
    public String login(String username, String password){
        //获取主体对象
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //执行登录方法
        try{
            subject.login(token);
        }catch (UnknownAccountException e){ //
            System.out.println("用户不存在");
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
        }
        return "登录成功";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "请先登录";
    }

    @RequestMapping("/noRole")
    public String noRole(){
        return "没有权限";
    }
    @RequestMapping("/front")
    public String front(){
        return "页面";
    }

}
